package com.chb.interceptor;

import com.alibaba.fastjson.JSON;
import com.chb.common.CommonConstants;
import com.chb.utils.UserInfoContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.slf4j.MDC;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @description: 设置请求头
 * @author: chb
 * @create: 2022-05-25 09:31
 **/
public class MyRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // fegin开启hystrix支持后会创建新线程导致追踪id变化，此处得到的追踪id为null
        requestTemplate.header(CommonConstants.TRACE_ID, MDC.get(CommonConstants.TRACE_ID));
        requestTemplate.header(CommonConstants.USER_INFO_HEADER_NAME, URLEncoder.encode(JSON.toJSONString(UserInfoContext.getUserInfo()), StandardCharsets.UTF_8));
    }
}
